﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using ATSIDAL;
using ATSModel;
using DBUtility;

namespace ATSDAL
{
    public class TTestItem : BaseRepository<TTestItemInfo>, ITTestItem
    {
        private const string PARM_TESTITEM_PRODUCTSN = "@ProductSN";
        private const string PARM_TESTITEM_TESTTIME = "@RecordTime";

        private const string PARM_TESTITEM_PSN = "@PSN";
        private const string PARM_TESTITEM_ANGLE = "@TestAngle";


        public IList<TTestItemInfo> LoadEntities()
        {
            return null;
        }

        public TTestItemInfo LoadEntity<T1>(T1 Id)
        {
            return null;
        }

        public IList<TTestItemInfo> GetTestItems(string productSN, DateTime testTime)
        {
            //Create a parameter    
            SqlParameter[] testItemParms = GetSNTParameters();
            //Bind the parameter
            testItemParms[0].Value = productSN;
            testItemParms[1].Value = testTime;
            return ExcuteSqlToGetEntities("TTestItem_select_SNDT", testItemParms);
        }

        #region TD 数据

        public DataSet GetTDInfo(string TableName, string Info,string PSN, string Angle)
        {
            DataSet ds=new DataSet();
            SqlParameter[] TDItemParms = GetTDTParameters(); 
            TDItemParms[0].Value = PSN;
            TDItemParms[1].Value = Angle;
            switch (Info)
            {
                case "TDVSWR":  //驻波
                    ds=ExcuteDataset("sp_selTDVSWRbySN", TableName, TDItemParms); 
                    break;
                case "TDISOS": //同频带间隔离
                    ds = ExcuteDataset("sp_selTDISOSbySN", TableName, TDItemParms);
                    break;
                case "TDISOD"://异频带间隔离
                    ds=ExcuteDataset("sp_selTDISODbySN", TableName, TDItemParms);
                    break;
                case "TDPhase"://相位
                    ds= ExcuteDataset("sp_selTDPhasebySN", TableName, TDItemParms);
                    break;
                case "TDMag"://幅度(耦合)
                    ds = ExcuteDataset("sp_selTDMagbySN", TableName, TDItemParms);
                    break;
                default:
                    break;
            }
            return ds;
        }

        public DataSet ExcuteDataset(string spname, string TableName, params SqlParameter[] parm)
        {
            DataSet set = new DataSet();
            set = sqlHelper.GetDataSet(spname, TableName, sqlHelper.ConnectionStringLocalTransaction, parm); 
            return set;
        } 
        private static SqlParameter[] GetTDTParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(PARM_TESTITEM_PSN, SqlDbType.NVarChar, 50),
					new SqlParameter(PARM_TESTITEM_ANGLE, SqlDbType.NVarChar, 50)                
                };
            return parms;
        }
        #endregion

        #region 参数部分

       

        private static SqlParameter[] GetSNTParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(PARM_TESTITEM_PRODUCTSN, SqlDbType.NVarChar, 50),
					new SqlParameter(PARM_TESTITEM_TESTTIME, SqlDbType.DateTime, 8)                
                };
            return parms;
        }
        #endregion

        #region 重写基类部分
        //-------------------------------------重写超类基础操作---------------------------------------------------------

        public override TTestItemInfo GetEntityRecord(SqlDataReader rdr)
        {
            TTestItemInfo pro = new TTestItemInfo
            {
                ProductSN = rdr.GetValue(0).ToString(),
                RecordTime = rdr.GetDateTime(1),
                TestItemName = rdr.GetValue(2).ToString(),
                TestTypeName = rdr.GetValue(3).ToString(),
                ////TestTypeID = rdr.GetInt32(4),
                //StartF = rdr.GetDecimal(4),
                //StopF = rdr.GetDecimal(5),
                VSWR = rdr.GetDecimal(4),
                ISO = rdr.GetDecimal(5),
                HighSpec = rdr.GetDecimal(6),
                LowSpec = rdr.GetDecimal(7),
                IsPass = rdr.GetValue(8).ToString(),
                TestAngle = rdr.GetValue(9).ToString(),
                TestBand = rdr.GetValue(10).ToString(),
                Phase = rdr.GetValue(11) is DBNull ? null : rdr.GetValue(11).ToString(),
                PhaseOffest = rdr.GetValue(12)is DBNull ? null : rdr.GetValue(12).ToString(),
                Mag = rdr.GetValue(13) is DBNull ? null : rdr.GetValue(13).ToString(),
                Am = rdr.GetValue(14) is DBNull ? null : rdr.GetValue(14).ToString(),
                Insertloss = rdr.GetValue(15)is DBNull ? null : rdr.GetValue(15).ToString(),
                FrePoint = rdr.GetValue(16) is DBNull ? null : rdr.GetValue(16).ToString()
            };
            return pro;
        }
        #endregion

    
    }
}